

import java.awt.MediaTracker;
import java.awt.Toolkit;
import java.awt.Image;

import org.havi.ui.HContainer;

public class ImagesController extends HContainer {
	
	private final String path = "data/";
	
	private static ImagesController instance = null;

	private ImagesController()
	{
	}
	
	public static ImagesController getInstance() {
		if (instance == null) {
			instance = new ImagesController();
		}
		return instance;
	}
	
	public Image getImage(String name) {
		if (name == null) {
			return null;
		}
		
		Image image = Toolkit.getDefaultToolkit().getImage(path + name);

		System.out.println("Carregando imagem: " + path + name + " ...");
		
		MediaTracker mediaTracker = new MediaTracker(this);
		mediaTracker.addImage(image,0);
	    try{
		      mediaTracker.waitForAll();
		    }
	    catch (InterruptedException ex){
	      image = null;
	      ex.printStackTrace();
	    }
	    if (mediaTracker.isErrorAny())
	      image = null;

		if (image == null)
			System.out.println("Imagem '" + name +"' ta null");
		else
			System.out.println("Imagem carregada.");

	    return image;

	}
}

